package com.example.novelstory.controller.lxy;

import com.example.novelstory.utill.Constants;
import com.example.novelstory.utill.Result;
import java.io.File;
import java.io.IOException;
import java.util.TimeZone;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/file")
@CrossOrigin
public class FileAction {
//文件必须使用post请求
    @PostMapping(value = "/upload")
    public Result upload(MultipartFile file) throws IOException{
        if (file.isEmpty()){
            return Result.error(Constants.CODE_600,"文件为空，上传失败！");
        }
        try {
//        前端上传的文件封装成一个对象
            File f =new File("/workspaces/upload/"  +file.getOriginalFilename());
//        获取字节流
            byte[] arr = file.getBytes();
            file.transferTo(f);
        }catch (IllegalStateException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success(Constants.CODE_200,"文件上传成功");
    }
}
